const app = getApp()
var util = require('../../utils/time.js')
var util2 = require('../../utils/time2.js')
Page({
    data: {
        showDate: false,
        dateFormat: '',
        currentDate: '',
        currentTime: '',
        id: '',
        showTime: false,
        place: '',
        people: '',
        waterType: '',
        color: '',
        smell: '',
        muddy: '',
        oil: '',
        note: '',
        receiveDateFormat: '',
        receiveTime: '',
        receivePeople: '',
        receiveNote: '',
        waterQuality: ''
    },
    onLoad: function (options) {
        let data = JSON.parse(options.data)
        this.setData({
            dateFormat: data['date'],
            currentDate: (new Date()).getTime(),
            currentTime: data['time'],
            id: data['id'],
            place: data['place'],
            people: data['people'],
            waterType: data['waterType'],
            color: data['color'],
            smell: data['smell'],
            muddy: data['muddy'],
            oil: data['oil'],
            note: data['note'],
            receiveDateFormat: this.formatDate(new Date()),
            receiveTime: util2.formatTime(new Date()),
            receivePeople: '',
            receiveNote: ''
        })
        console.log(this.data)
    },
    onChangeID(event) {
        this.setData({
            id: event.detail
        })
    },
    onChangePlace(event) {
        this.setData({
            place: event.detail
        })
    },
    onChangePeople(event) {
        this.setData({
            people: event.detail
        })
    },
    onChangeWaterType(event) {
        this.setData({
            waterType: event.detail
        })
    },
    onChangeColor(event) {
        this.setData({
            color: event.detail
        })
    },
    onChangeSmell(event) {
        this.setData({
            smell: event.detail
        })
    },
    onChangeMuddy(event) {
        this.setData({
            muddy: event.detail
        })
    },
    onChangeOil(event) {
        this.setData({
            oil: event.detail
        })
    },
    onChangeNote(event) {
        this.setData({
            note: event.detail
        })
    },
    onChangeReceiveDate(event) {
        this.setData({
            receiveDate: event.detail
        })
    },
    onChangeReceiveTime(event) {
        this.setData({
            receiveTime: event.detail
        })
    },
    onChangeReceivePeople(event) {
        this.setData({
            receivePeople: event.detail
        })
    },
    onChangeReceiveNote(event) {
        this.setData({
            receiveNote: event.detail
        })
    },
    onChangeWaterQuality(event) {
        this.setData({
            waterQuality: event.detail
        })
    },
    CloseDate() {
        this.setData({
            showDate: false
        });
    },
    DisplayDate() {
        this.setData({
            showDate: true
        });
    },
    formatDate(date) {
        return `${date.getFullYear()}/${(date.getMonth())+1}/${date.getDate()}`;
    },
    setDateValue(event) {
        this.setData({
            showDate: false,
            currentDate: event.detail,
            receiveDateFormat: (new Date(event.detail)).toLocaleDateString()
        });
        this.CloseDate()
    },
    getID() {
        wx.scanCode({
            success: (res) => {
                this.setData({
                    id: res.result
                })
            }
        })
    },
    CloseTime(event) {
        this.setData({
            showTime: false
        })
    },
    DisplayTime() {
        this.setData({
            showTime: true
        })
    },
    setTimeValue(event) {
        this.setData({
            receiveTime: event.detail
        })
        this.CloseTime()
    },
    submit() {
        let SumbitData = this.data
        let date = ''
        let time = ''
        let receiveDate = ''
        for (var key in SumbitData) {
            if (key == 'dateFormat') {
                date = SumbitData[key]
            }
            if (key == 'currentTime') {
                time = SumbitData['currentTime']
            }
            if (key == 'receiveDateFormat') {
                receiveDate = SumbitData['receiveDateFormat']
            }
        }
        SumbitData['date'] = date
        SumbitData['time'] = time
        SumbitData['receiveDate'] = receiveDate
        SumbitData['complete'] = true
        // 去除SumbitData中的showDate、showTime、currentDate
        delete SumbitData['showDate']
        delete SumbitData['showTime']
        delete SumbitData['currentDate']
        delete SumbitData['receiveDateFormat']
        delete SumbitData['currentTime']
        delete SumbitData['dateFormat']
        delete SumbitData['__webviewId__']
        console.log("!!!", SumbitData)
        //检查是否存在空值
        for (var i in SumbitData) {
            if (!SumbitData[i]) {
                wx.showToast({
                    title: '信息未填写完整',
                    icon: 'error',
                    duration: 2000
                })
                return;
            }
        }
        wx.request({
            // url: `http://127.0.0.1:5000/updateSample`,
            url: app.api + "updateSample",
            data: SumbitData,
            method: 'POST',
            header: {
                'content-type': 'application/json' // 默认值
            },
            success(res) {
                console.log(res)
                wx.navigateBack()
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 2000
                })
            },
            fail(res) {
                console.log(res)
                wx.showToast({
                    title: '修改样品失败',
                    icon: 'error',
                    duration: 2000
                })
            }
        })
    }
});